// src/router/index.js
import { createRouter, createWebHashHistory } from 'vue-router';
import { getDeviceList } from "@/api/deviceApi";


// 创建路由器实例
const menuRouter = createRouter({
  history: createWebHashHistory(),
  routes: [{
    path: `/`,
    name: "",
    redirect: "/device/"
  }],
});

await setUpRoutes();

// 动态添加路由的函数
export async function setUpRoutes() {
  try {
    const deviceList = await getDeviceList();
    // 将动态路由添加到路由器实例中
    for (const device of deviceList) {
      const deviceName = device.name;
      const route = {
        path: `/device/${deviceName}`,
        name: deviceName,
        component: () => import('../views/DeviceView.vue')
      };
      menuRouter.addRoute(route);
    }
  } catch (error) {
    console.error('Error adding dynamic routes:', error);
  }
}

export default menuRouter;